<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="ordersId">
        <el-input
          v-model="queryParams.ordersId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员编号" prop="peoId">
        <el-input
          v-model="queryParams.peoId"
          placeholder="请输入人员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物编号" prop="item2Id">
        <el-input
          v-model="queryParams.item2Id"
          placeholder="请输入货物编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购日期" prop="datePurch">
        <el-input
          v-model="queryParams.datePurch"
          placeholder="请输入采购日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达日期" prop="dateArriv">
        <el-input
          v-model="queryParams.dateArriv"
          placeholder="请输入到达日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商编号" prop="manuName">
        <el-input
          v-model="queryParams.manuName"
          placeholder="请输入厂商编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否合格" prop="ifQualified">
        <el-input
          v-model="queryParams.ifQualified"
          placeholder="请输入是否合格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:procure:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:procure:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:procure:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:procure:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <br><br>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">订单管理</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">首页</a></el-breadcrumb-item>
      <el-breadcrumb-item>订单查询</el-breadcrumb-item>
      <el-breadcrumb-item>订单详情</el-breadcrumb-item><br/>
      <el-divider></el-divider>
      <el-breadcrumb-item>
        <h1>订单采购详情</h1>
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-steps :active="1">
      <el-step title="采购下单" description="2024.05.06"></el-step>
      <el-step title="采购商发货" description="2024.05.10"></el-step>
      <el-step title="收货" description="2024.05.24"></el-step>
    </el-steps>
    <el-divider></el-divider>
    <el-descriptions title="采购人员详情" direction="vertical" :column="4" border>
      <el-descriptions-item label="人员姓名">Elainasan</el-descriptions-item>
      <el-descriptions-item label="人员编号">1</el-descriptions-item>
      <el-descriptions-item label="所属部门" :span="2">供应部</el-descriptions-item>
      <el-descriptions-item label="备注信息">
        <el-tag size="small">三年采购经验</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="采购详情">负责采购1000箱牢大冰红茶
        <div class="demo-image">
        <div class="block" v-for="fit in fits" :key="fit">
          <span class="demonstration"></span>
          <el-image
            style="width: 100px; height: 100px"
            :src="url"
            :fit="fit"></el-image>
        </div>
      </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-divider></el-divider>
    <el-descriptions title="供应商详情" direction="vertical" :column="4" border>
      <el-descriptions-item label="供应商名称">玛尔沃专供一厂</el-descriptions-item>
      <el-descriptions-item label="厂商编号">001</el-descriptions-item>
      <el-descriptions-item label="备注信息">
        冰露不足，其余库存充足
      </el-descriptions-item>
      <el-descriptions-item label="物品质量">合格品</el-descriptions-item>
    </el-descriptions>
    <el-divider></el-divider>
    <el-descriptions title="采购货物详情" direction="vertical" :column="4" border/>
    <el-table v-loading="loading" :data="procureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="ordersId" />
      <el-table-column label="人员编号" align="center" prop="peoId" />
      <el-table-column label="货物编号" align="center" prop="item2Id" />
      <el-table-column label="采购日期" align="center" prop="datePurch" />
      <el-table-column label="到达日期" align="center" prop="dateArriv" />
      <el-table-column label="厂商编号" align="center" prop="manuName" />
      <el-table-column label="是否合格" align="center" prop="ifQualified" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:procure:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:procure:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员编号" prop="peoId">
          <el-input v-model="form.peoId" placeholder="请输入人员编号" />
        </el-form-item>
        <el-form-item label="货物编号" prop="item2Id">
          <el-input v-model="form.item2Id" placeholder="请输入货物编号" />
        </el-form-item>
        <el-form-item label="采购日期" prop="datePurch">
          <el-input v-model="form.datePurch" placeholder="请输入采购日期" />
        </el-form-item>
        <el-form-item label="到达日期" prop="dateArriv">
          <el-input v-model="form.dateArriv" placeholder="请输入到达日期" />
        </el-form-item>
        <el-form-item label="厂商编号" prop="manuName">
          <el-input v-model="form.manuName" placeholder="请输入厂商编号" />
        </el-form-item>
        <el-form-item label="是否合格" prop="ifQualified">
          <el-input v-model="form.ifQualified" placeholder="请输入是否合格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcure, getProcure, delProcure, addProcure, updateProcure } from "@/api/system/procure";

export default {
  name: "Procure",
  data() {
    return {
      fits: ['实物图片：'],
      url: 'https://imgservice.suning.cn/uimg1/b2c/image/ZPocgxRwNwd9OiARk_CaKg.jpg',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购信息表格数据
      procureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ordersId: null,
        peoId: null,
        item2Id: null,
        datePurch: null,
        dateArriv: null,
        manuName: null,
        ifQualified: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ordersId: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        peoId: [
          { required: true, message: "人员编号不能为空", trigger: "blur" }
        ],
        item2Id: [
          { required: true, message: "货物编号不能为空", trigger: "blur" }
        ],
        datePurch: [
          { required: true, message: "采购日期不能为空", trigger: "blur" }
        ],
        dateArriv: [
          { required: true, message: "到达日期不能为空", trigger: "blur" }
        ],
        manuName: [
          { required: true, message: "厂商编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购信息列表 */
    getList() {
      this.loading = true;
      listProcure(this.queryParams).then(response => {
        this.procureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ordersId: null,
        peoId: null,
        item2Id: null,
        datePurch: null,
        dateArriv: null,
        manuName: null,
        ifQualified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordersId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ordersId = row.ordersId || this.ids
      getProcure(ordersId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ordersId != null) {
            updateProcure(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcure(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordersIds = row.ordersId || this.ids;
      this.$modal.confirm('是否确认删除采购信息编号为"' + ordersIds + '"的数据项？').then(function() {
        return delProcure(ordersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/procure/export', {
        ...this.queryParams
      }, `procure_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
